<?php
include_once 'data.php';
include_once 'functions.php';
require_once('lib/simple_html_dom.php');
//Include google API
require_once('lib/google_api/src/apiClient.php');
require_once('lib/google_api/src/contrib/apiCustomsearchService.php');



if(!empty($_FILES)) {
	
    if(isset($_GET['proxy_name'])) $proxy_name = $_GET['proxy_name'];
    if(isset($_GET['proxy_port'])) $proxy_port = $_GET['proxy_port'];
    if(isset($_GET['proxy_username'])) $proxy_username = $_GET['proxy_username'];
    if(isset($_GET['proxy_password'])) $proxy_password = $_GET['proxy_password'];
    if (!empty($_GET['user'])) $user = $_GET['user'];
    if (!empty($_GET['userID'])) $userID = $_GET['userID'];

            $database_pubmed = '';
            $database_nasaads = '';
            $database_crossref = '';
            $failed = '';

            if(isset($_GET['database_pubmed'])) $database_pubmed = $_GET['database_pubmed'];
            if(isset($_GET['database_nasaads'])) $database_nasaads = $_GET['database_nasaads'];
            if(isset($_GET['database_crossref'])) $database_crossref = $_GET['database_crossref'];
            if(isset($_GET['failed'])) $failed = $_GET['failed'];

            database_connect($usersdatabase_path, 'users');
            save_setting($dbHandle,'batchimport_database_pubmed',$database_pubmed);
            save_setting($dbHandle,'batchimport_database_nasaads',$database_nasaads);
            save_setting($dbHandle,'batchimport_database_crossref',$database_crossref);
            save_setting($dbHandle,'batchimport_failed',$failed);
            $dbHandle = null;

    session_write_close();

    $stopwords = "a's, able, about, above, according, accordingly, across, actually, after, afterwards, again, against, ain't, all, allow, allows, almost, alone, along, already, also, although, always, am, among, amongst, an, and, another, any, anybody, anyhow, anyone, anything, anyway, anyways, anywhere, apart, appear, appreciate, appropriate, are, aren't, around, as, aside, ask, asking, associated, at, available, away, awfully, be, became, because, become, becomes, becoming, been, before, beforehand, behind, being, believe, below, beside, besides, best, better, between, beyond, both, brief, but, by, c'mon, c's, came, can, can't, cannot, cant, cause, causes, certain, certainly, changes, clearly, co, com, come, comes, concerning, consequently, consider, considering, contain, containing, contains, corresponding, could, couldn't, course, currently, definitely, described, despite, did, didn't, different, do, does, doesn't, doing, don't, done, down, downwards, during, each, edu, eg, eight, either, else, elsewhere, enough, entirely, especially, et, etc, even, ever, every, everybody, everyone, everything, everywhere, ex, exactly, example, except, far, few, fifth, figure, figures, first, five, followed, following, follows, for, former, formerly, forth, four, from, further, furthermore, get, gets, getting, given, gives, go, goes, going, gone, got, gotten, greetings, had, hadn't, happens, hardly, has, hasn't, have, haven't, having, he, he's, hello, help, hence, her, here, here's, hereafter, hereby, herein, hereupon, hers, herself, hi, him, himself, his, hither, hopefully, how, howbeit, however, i'd, i'll, i'm, i've, ie, if, ignored, immediate, in, inasmuch, inc, indeed, indicate, indicated, indicates, inner, insofar, instead, into, inward, is, isn't, it, it'd, it'll, it's, its, itself, just, keep, keeps, kept, know, knows, known, last, lately, later, latter, latterly, least, less, lest, let, let's, like, liked, likely, little, look, looking, looks, ltd, mainly, many, may, maybe, me, mean, meanwhile, merely, might, more, moreover, most, mostly, much, must, my, myself, name, namely, nd, near, nearly, necessary, need, needs, neither, never, nevertheless, new, next, nine, no, nobody, non, none, noone, nor, normally, not, nothing, novel, now, nowhere, obviously, of, off, often, oh, ok, okay, old, on, once, one, ones, only, onto, or, other, others, otherwise, ought, our, ours, ourselves, out, outside, over, overall, own, particular, particularly, per, perhaps, placed, please, plus, possible, presumably, probably, provides, que, quite, qv, rather, rd, re, really, reasonably, regarding, regardless, regards, relatively, respectively, right, said, same, saw, say, saying, says, second, secondly, see, seeing, seem, seemed, seeming, seems, seen, self, selves, sensible, sent, serious, seriously, seven, several, shall, she, should, shouldn't, since, six, so, some, somebody, somehow, someone, something, sometime, sometimes, somewhat, somewhere, soon, sorry, specified, specify, specifying, still, sub, such, sup, sure, t's, table, tables, take, taken, tell, tends, th, than, thank, thanks, thanx, that, that's, thats, the, their, theirs, them, themselves, then, thence, there, there's, thereafter, thereby, therefore, therein, theres, thereupon, these, they, they'd, they'll, they're, they've, think, third, this, thorough, thoroughly, those, though, three, through, throughout, thru, thus, to, together, too, took, toward, towards, tried, tries, truly, try, trying, twice, two, un, under, unfortunately, unless, unlikely, until, unto, up, upon, us, use, used, useful, uses, using, usually, value, various, very, via, viz, vs, want, wants, was, wasn't, way, we, we'd, we'll, we're, we've, welcome, well, went, were, weren't, what, what's, whatever, when, whence, whenever, where, where's, whereafter, whereas, whereby, wherein, whereupon, wherever, whether, which, while, whither, who, who's, whoever, whole, whom, whose, why, will, willing, wish, with, within, without, won't, wonder, would, would, wouldn't, yes, yet, you, you'd, you'll, you're, you've, your, yours, yourself, yourselves, zero";

    $stopwords = explode (', ', $stopwords);

    $patterns = join("\b/ui /\b", $stopwords);
    $patterns = "/\b$patterns\b/ui";
    $patterns = explode(" ", $patterns);

    $order   = array("\r\n", "\n", "\r");

    
    $log = "";
    $html_log = "";
    $html_log_2 = "";
    
    $timestamp1 = date("Y-m-d H:i:s");
    $log .="Batch Import started at ".$timestamp1."\n\n";
    $no_success = 0;
    $no_failed = 0;
    $starttime = time();
    
     if (isset($_FILES['files'])) {
            for ($i = 0; $i < count($_FILES['files']['name']); $i++) {
                if (is_uploaded_file($_FILES['files']['tmp_name'][$i])) {
	
			       $file = $_FILES['files']['tmp_name'][$i];
                    $orig_filename = $_FILES['files']['name'][$i];
                    $timestamp = date("Y-m-d H:i:s");
					
                    $not_found = false;
			       
       				 if (is_readable($file)) {

			            $string = '';
			            $xml = '';
			            $record = '';
			            $count = '';
			            $url = '';
			            $authors = '';
			            $authors_array = array();
			            $affiliation = '';
			            $title = '';
			            $abstract = '';
			            $secondary_title = '';
			            $year = '';
			            $volume = '';
			            $issue = '';
			            $pages = '';
			            $last_page = '';
			            $journal = '';
			            $keywords = '';
			            $name_array =  array();
			            $mesh_array =  array();
			            $new_file = '';
			            $journal = '';
			            $addition_date = date('Y-m-d');
			            $rating = 2;
			            $uid = '';
			            $editor = '';
			            $reference_type = '';
			            $publisher = '';
			            $place_published = '';
			            $doi = '';
			            $authors_ascii = '';
			            $title_ascii = '';
			            $abstract_ascii = '';
			            $unpacked_files = array ();

			            
			            
            			if (file_exists($temp_dir.DIRECTORY_SEPARATOR."librarian_temp".$i.".txt")) unlink ($temp_dir.DIRECTORY_SEPARATOR."librarian_temp".$i.".txt");

            			
            			##########	extract text from pdf	##########

            			system(select_pdftotext().'"'.$file.'" "'.$temp_dir.DIRECTORY_SEPARATOR.'librarian_temp'.$i.'.txt"', $ret);

            			
           				if (file_exists($temp_dir.DIRECTORY_SEPARATOR."librarian_temp".$i.".txt")) 
           					$string = file_get_contents ($temp_dir.DIRECTORY_SEPARATOR."librarian_temp".$i.".txt");

            			if (empty($string)) {
            				
                	
	                		if (isset($_POST['failed']) && $_POST['failed']=='1') {
	
		                    database_connect($database_path, 'library');
		                    record_unknown($dbHandle, $string, $database_path, $file, $userID);
		                    
		                    $saved = copy($file, $library_path . "batch" . DIRECTORY_SEPARATOR . basename($orig_filename));
		                   // $put = basename($orig_filename).": Recorded as unknown. Full text not indexed (copying disallowed).<br>";
		                    $html_log_2 .= "<li>" . basename($orig_filename) . ": Recorded as unknown. Full text not indexed (copying disallowed).</li>";
		                    $log .= "[" . $timestamp . "] " . basename($orig_filename) . ": Recorded as unknown. Full text not indexed (copying disallowed).\n";
		                    $no_success ++;
			                } else {
			                	
			                    //$put = basename($orig_filename).": copying disallowed.<br>";
			                	if (isset($_POST['failed']) && $_POST['failed']=='2'){
			                		copy ($file, dirname(__FILE__).DIRECTORY_SEPARATOR."library".DIRECTORY_SEPARATOR."batch".DIRECTORY_SEPARATOR.basename($orig_filename));
			                		$html_log_2 .= "<li>" . basename($orig_filename) . ": copying disallowed. Saved to batch directory.</li>";
			                		$log .= "[" . $timestamp . "] " . basename($orig_filename) . ": copying disallowed. Saved to batch directory.\n";
			                		$no_success ++;
			                	}else{
			                		$html_log_2 .= "<li>" . basename($orig_filename) . ": copying disallowed.</li>";
			                		$log .= "[" . $timestamp . "] " . basename($orig_filename) . ": copying disallowed.\n";
			                		$no_failed ++;
			                	}
			                }

            			} else {
            				
            				##### @author Evica Ilieva
            				//Put every line from the file into a single entry in a array
            				$string_array = explode("\n", $string);
            				
            				
            				//Trim the array. Delete the empty rows and trim the values in the array
            				$string_array = array_map('trim', $string_array);
            				
            				#### Relevant for Requirements ILF002, ILF003, ILF015
            				//Get the title of the paper from the PDF
            				$title_pdf = $string_array[0];
            				//echo $title_pdf."<br>";
            				#### Relevant for Requirement ILF017
            				//Search for keywords inside the PDF
            				$keywords_search = preg_grep("/key\s*words:/i", $string_array);
            				if (empty($keywords_search))
            					$keywords_search = preg_grep("/terms/i", $string_array);
            				
            				//print "<pre>" . print_r($keywords_search, true) . "</pre>";
            				if (!empty($keywords_search)) {
            				
            					$keywords_array = array();
            					$m = 0;
            					$keywords_keys = array_keys($keywords_search);
            					foreach (array_keys($keywords_search) as $key) {
            						$k = $key;
            						//if it contains keywords, then it is the next row
            						if (stripos($string_array[$k], "keywords") !== false) {
            							$k++;
            						} elseif (stripos($string_array[$k], "terms") !== false) {
            							$k++;
            						}
            				
            						if (empty($string_array[$k]))
            							$k++;
            				
            						$keyword_string = $string_array[$k];
            				
            						$keyword_string = trim($keyword_string, "keywords:");
            						$keyword_string = trim($keyword_string, "keyword:");
            						$keyword_string = trim($keyword_string, "Keywords:");
            						$keyword_string = trim($keyword_string, "Keywords");
            						$keyword_string = trim($keyword_string, "key words:");
            						$keyword_string = trim($keyword_string, "Key words:");
            						$keyword_string = trim($keyword_string, "Key words");
            						$keyword_string = trim($keyword_string, "key words");
            						$keyword_string = trim($keyword_string, "Terms");
            						$keyword_string = trim($keyword_string, "Terms:");
            						$keyword_string = trim($keyword_string, "terms:");
            						$keyword_string = trim($keyword_string, "terms");
            				
            				
            						$tmp_arr = preg_split("/[-,;|�]/", $keyword_string);
            						foreach ($tmp_arr as $k) {
            							$keywords_array[$m] = trim($k);
            							$m++;
            						}
            					}
            				}
            				
            				if(!empty($keyword_array)) $keywords = join (" / ", $keywords_array);
            				
            				
            				#### To be used when the abstract from the Document object is not populated. Good case is when extracting metadata from ACM.
            				//Search fro abstract inside the PDF
            				$abstract_search = preg_grep("/abstract*/i", $string_array);
            				$search_keys = array_keys($abstract_search);
            				
            				$abstract = "";
            				if (!empty($search_keys)) {
            					if (strlen($string_array[$search_keys[0]]) > 100) {
            						$abstract = $string_array[$search_keys[0]];
            						$k = $search_keys[0];
            					} else {
            						$abstract = $string_array[$search_keys[0] + 1];
            						$k = $search_keys[0] + 1;
            					}
            					if (empty($string_array[$k]))
            						$k++;
            				
            					while (!empty($string_array[$k])) {
            						$abstract.=$string_array[$k];
            						$k++;
            					}
            				}
            				
            				
			                $string = preg_replace ('/[^[\x09\x0A\x0D\x20-\x7E]|[\xC2-\xDF][\x80-\xBF]|\xE0[\xA0-\xBF][\x80-\xBF]|[\xE1-\xEC\xEE\xEF][\x80-\xBF]{2}|\xED[\x80-\x9F][\x80-\xBF]|\xF0[\x90-\xBF][\x80-\xBF]{2}|[\xF1-\xF3][\x80-\xBF]{3}|\xF4[\x80-\x8F][\x80-\xBF]{2}]/', ' ', $string);
			                $string = str_replace($order, ' ', $string);
			                $order   = array("\xe2\x80\x93", "\xe2\x80\x94");
			                $replace = '-';
			                $string = str_replace ($order, $replace, $string);
			
			                preg_match_all ('/10\.\d{4}\/\S+/ui', $string, $doi, PREG_PATTERN_ORDER);

			                
                		if (count($doi[0]) < 1) {

                    		if (isset($_POST['failed']) && $_POST['failed']=='1') {

		                        $string = preg_replace ($patterns, ' ', $string);
		                        $string = preg_replace ('/(^|\s)\S{1,2}(\s|$)/u', ' ', $string);
		                        $string = preg_replace ('/\s{2,}/u', " ", $string);
		
		                        $fulltext_array = array();
		                        $fulltext_unique = array();
		
		                        $fulltext_array = explode(" ", $string);
		                        $fulltext_unique = array_unique($fulltext_array);
		                        $string = implode(" ", $fulltext_unique);
		
		                        //database_connect($database_path, 'library');
		                        //record_unknown($dbHandle, $string, $database_path, $file, $userID);
								
		                        $not_found = true;
		                        
		                        //$put = basename($orig_filename).": Recorded as unknown. DOI not found.<br>";

                    		} else {
                    			if (isset($_POST['failed']) && $_POST['failed']=='2'){
                    				$saved = copy ($file, dirname(__FILE__).DIRECTORY_SEPARATOR."library".DIRECTORY_SEPARATOR."batch".DIRECTORY_SEPARATOR.basename($orig_filename));
                    				if($saved){
	                    				$no_success ++;
	                    				
	                    				$html_log_2 .= "<li>" . basename($orig_filename) . ": Not Recorded. File saved to the batch directory. DOI not found.</li>";
	                    				$log .= "[" . $timestamp . "] " . basename($orig_filename) . ": Not Recorded. File saved to the batch directory. DOI not found.\n";
                    				}else{
                    					$html_log_2 .= "<li>" . basename($orig_filename) . ": Not recorded. File could not be saved!</li>";
                    					
                    					$log .= "[" . $timestamp . "] " . basename($orig_filename) . ": Not recorded. File could not be saved!\n";
                    					$no_failed ++;
                    					 
                    				}
                    			}
                    			
                       			 //$put = basename($orig_filename).": DOI not found.<br>";
                    			
                    		}

                	} else {
                		
                    	$doi = $doi[0][0];
                    	
                    	if(substr($doi, -1) == ')' || substr($doi, -1) == ']') {
                        	preg_match_all ('/(.)(doi:\s?)?(10\.\d{4}\/\S+)/ui', $string, $doi2, PREG_PATTERN_ORDER);
                        	if (substr($doi, -1) == ')' && $doi2[1][0] == '(') $doi = substr($doi, 0, -1);
                        	if (substr($doi, -1) == ']' && $doi2[1][0] == '[') $doi = substr($doi, 0, -1);
                    	}

                    	$title = '';
		
                    	foreach ($_POST['database'] as $database) {
                    		switch ($database) {
                    			case "ieee": $ieee = true;
                    			break;
                    			case "acm" : $acm = true;
                    			break;
                    			case "springerlink" : $springerlink = true;
                    			break;
                    			case "pubmed" : $pubmed = true;
                    			break;
                    			case "nasa" : $nasa = true;
                    			break;
                    			case "crossref" : $crossref = true;
                    			break;
                    		}
                   	 }
                    	
                   	
                   	 
                   if(empty($title) && $ieee && !empty($doi)){
                   	$request_url = "http://ieeexplore.ieee.org/gateway/ipsSearch.jsp?doi=".urlencode($doi);
                   	
                   	$xml = proxy_simplexml_load_file($request_url, $proxy_name, $proxy_port, $proxy_username, $proxy_password);
                   	if (empty($xml)) $count = 0;
                   	
                   	if(!isset($count))
                   		$count = $xml->totalfound;
                   	 
                   	$record = $xml->document;
                   	
                   	if(!empty($record->title))
                   		$title = (string)$record->title;
                   	
                   	if(isset($record->doi))
                   		$doi = $record->doi;
                   	if(!empty($record->py)){
                   		$pub_date = $record->py."-01-01";
                   		$year = $pub_date;
                   	}
                   	if(!empty($record->affiliations))
                   		$affiliation = $record->affiliations;
                   	//$_POST['secondary_title'] = $record->pubtitle;
					if(!empty( $record->pubtitle))
                   		$journal = $record->pubtitle;
                   	if(!empty($record->issue))
						$issue = $record->issue;
                   	if(!empty($record->volume))
                   		$volume = $record->volume;
					if(!empty($record->spage) && !empty($record->epage))
                  	 	$pages = $record->spage."-".$record->epage;
                   	if(!empty($record->publisher))
						$publisher = $record->publisher;
					if(!empty($record->pubtype))
                   		$reference_type = $record->pubtype;
					//else
						$reference_type = '';
					if(!empty($record->abstract))
                   	$abstract = trim($record->abstract);
                   	 
					if(!empty($record->authors))
                   		$authors = (string)$record->authors;
                   	 
                   	$name_array = array();
                   	if (!empty($authors)) {
                   	
                   		$author_array = explode(';', $authors);
                   		foreach($author_array as $author){
                   			$exp = explode(',',$author);
                   			$last = trim(array_pop($exp));
                   			$first = trim(array_pop($exp));
                   			$name_array[] = 'L:"'.$last.'",F:"'.$first.'"';
                   		}
                   	}
                   	
                   	if (isset($name_array))
                   		$authors = join(";", $name_array);
                   	 
                   	$m=0;
                   	$suggested_keywords = array();
                   	if(isset($xml->document->controlledterms)){
                   		foreach($xml->document->controlledterms->term as $term){
                   			$suggested_keywords[$m] = (string)$term;
                   			$m++;
                   		}
                   	}
                   	
                   	
                   	if(isset($xml->document->thesaurusterms)){
                   		foreach($xml->document->thesaurusterms->term as $term){
                   			if(!in_array((string)$term, $suggested_keywords)){
                   				$suggested_keywords[$m]=(string)$term;
                   			}
                   			$m++;
                   		}
                   	}
                   	 
                   	if(isset($xml->document->uncontrolledterms)){
                   		foreach($xml->document->uncontrolledterms->term as $term){
                   			if(!in_array((string)$term, $suggested_keywords)){
                   				$suggested_keywords[$m]=(string)$term;
                   			}
                   			$m++;
                   		}
                   	}
                   	
                   	if(isset($suggested_keywords)) $keywords = join (" / ", $suggested_keywords);
                   	
                   	if(!empty($title)){
                   		$uid = "IEEE:$record->arnumber";
                   		$url = (string)$record->mdurl;
                   		//$_POST['form_new_file_link'] = (string)$record->pdf;
                   	}
                   } 	
                    	
                  
                   
                   
                   if(empty($title) && $acm && !empty($doi)){
                   	if(!empty($doi))
                   		// Trim the doi to omit the part before "/"
                   		if (stripos($doi, '/') > 0) $acm_id = trim(substr($doi,stripos($doi, '/')+1));
                   	
                   	$client = new apiClient();
                   	$client->setApplicationName('Google CustomSearch PHP Starter Application');
                   	// Docs: http://code.google.com/apis/customsearch/v1/using_rest.html
                   	// Visit https://code.google.com/apis/console?api=customsearch to generate
                   	// your developer key (simple api key).
                   	$client->setDeveloperKey('AIzaSyCnyVXlI3vtzXji5Skyq2tjB151GKuxHTM');
                   	$search = new apiCustomsearchService($client);
                   	 
                   	try{
                   		//Get the result from the Google Search
                   		$result = $search->cse->listCse($doi, array(
                   				'cx' => '003464685527199486674:rgmxgh34f6u', // The custom search engine ID to scope this search query.
                   		));
                   	} catch (apiServiceException $e) {
                   		// Handle exception. You can also catch Exception here.
                   		// You can also get the error code from $e->getCode();
                   		//die($e->getCode());
                   	}
                   	
                   	//Get the metadata from the search.
                   	// nb! levenshtein function allows 0 symbols to differ
                   	// Daniil: MODIFICATION BEGIN
                   	$j = 0;
                   	$n=$result['queries']['request'][0]['count'];
                   	 
                   	while ($j<$n){
                   		//echo $result['items'][$i];
                   		//echo $i;
                   		$metadata_array = $result['items'][$j]['pagemap']['metatags'][0];
                   		 
                   		//var_dump($metadata_array);
                   		if(isset($metadata_array['citation_doi'])){
                   			$doi_search = $metadata_array['citation_doi'];
                   			$uid_search = substr($metadata_array['citation_doi'], strpos($metadata_array['citation_doi'],"/")+1);
                   		}
                   		 
                   	
                   		if (levenshtein($doi_search, $doi)==0 || (!empty($uid) && levenshtein($uid_search,$uid)==0)) {
                   			// Daniil: MODIFICATION END
                   			$uid = "ACM:$acm_id";
                   			 
                   			$count =1;
                   			if(isset($metadata_array['citation_title'])){
                   				$title= $metadata_array['citation_title'];
                   			}
                   			if(isset($metadata_array['citation_authors'])){
                   				$authors = $metadata_array['citation_authors'];
                   	
                   				$name_array = array();
                   				if (!empty($authors)) {
                   	
                   					$author_array = explode(';', $authors);
                   					foreach($author_array as $author){
                   						$exp = explode(',',$author);
                   						$first = trim(array_pop($exp));
                   						$last = trim(array_pop($exp));
                   						$name_array[] = 'L:"'.$last.'",F:"'.$first.'"';
                   					}
                   				}
                   	
                   				if (isset($name_array))
                   					$authors = join(";", $name_array);
                   			}
                   			 
                   			if(isset($metadata_array['citation_journal_title']))
                   				$yournal = $metadata_array['citation_journal_title'];
                   			if(isset($metadata_array['citation_date']))
                   				$year = date('Y-m-d',strtotime($metadata_array['citation_date']));
                   			if(isset($metadata_array['citation_volume']))
                   				$volume = $metadata_array['citation_volume'];
                   			if(isset($metadata_array['citation_issue']))
                   				$issue = $metadata_array['citation_issue'];
                   			if(isset($metadata_array['citation_firstpage']))
                   				$tmp_pages = $metadata_array['citation_firstpage'];
                   			if(isset($metadata_array['citation_lastpage']))
                   				$tmp_pages = $tmp_pages."-".$metadata_array['citation_lastpage'];
                   			$pages= $tmp_pages;
                   			 
                   			if(isset($metadata_array['citation_publisher']))
                   				$publisher = $metadata_array['citation_publisher'];
                   			if(isset($metadata_array['citation_doi']))
                   				$doi = $metadata_array['citation_doi'];
                   			
                   			if(isset($metadata_array['citation_abstract_html_url']))
                   				$url = $metadata_array['citation_abstract_html_url'];
                   	
                   			$suggested_keywords = array();
                   			if(isset($metadata_array['citation_keywords'])){
                   				$keyword_array = explode($keyword_separator, $metadata_array['citation_keywords']);
                   				//function trim_value(&$value) {
                   				//	$value = trim($value);
                   				//}
                   				array_walk($keyword_array, 'trim_value');
                   				$m=0;
                   				foreach ($keyword_array as $k){
                   					$suggested_keywords[$m] = $k;
                   					$m++;
                   				}
                   	
                   			}
                   			if (isset($suggested_keywords)) $keywords = join (" / ", $suggested_keywords);
                   			 
                   		}
                   	
                   		$j++;
                   	}
                   }
                   
                   if(empty($title) && $springerlink && !empty($doi)){
                   	
                   	$request_url = "http://www.springerlink.com/content/?k=doi:(".$doi.")";
                   	 
                   	// Create DOM from URL or file
                   	$html = file_get_html($request_url);
                   	 
                   	 
                   	// Find the page of the article for given doi
                   	foreach($html->find('ul[id=PrimaryManifest]') as $ul)
                   	{
                   		foreach($ul->find('li') as $li)
                   		{
                   			foreach($li->find('p.title a') as $p){
                   				$page=$p->href;
                   			}
                   		}
                   	}
                   	 
                   	$html = null;
                   	 
                   	$request_url = "http://springerlink.com".$page;
                   	// Create DOM from URL or file
                   	$html = file_get_html($request_url);
                   	 
                   	$doi1 = $html->find('span.doi span.value');
                   	if(!empty($doi1))
                   		$doi = $doi1[0]->plaintext;
                   	
                   	$title1 = $html->find('div.text h1 a');
                   	if(!empty($title1)){
                   		$title = $title1[0]->plaintext;
                   		$count = 0;
                   	}
                   	
                   	$journal1 = $html->find('div.primary a');
                   	if(!empty($journal1))
                   		$journal = $journal1[0]->plaintext;
                   	
                   	$pages1 = $html->find('span.pagination');
                   	if(!empty($pages1))
                   		$pages = $pages1[0]->plaintext;
                   	
                   	//Get the authors
                   	$authors_array = array();
                   	foreach($html->find('div.text p.authors') as $d){
                   		//$doi=$d->find('span');
                   		foreach($d->children as $c)
                   			$authors_array[]=$c->plaintext."<br>";
                   	}
                   	$authors1 = implode('; ', $authors_array);
                   	$authors1 = str_replace("<br>", "", $authors);
                   	 
                   	$name_array = array();
                   	if (!empty($authors1)) {
                   		 
                   		$author_array = explode(';', $authors1);
                   		foreach($author_array as $author1){
                   			$exp = explode(' ',$author);
                   			$first = trim(array_pop($exp));
                   			$last = trim(array_pop($exp));
                   			$name_array[] = 'L:"'.$last.'",F:"'.$first.'"';
                   		}
                   	}
                   	 
                   	if (isset($name_array))
                   		$authors = join(";", $name_array);
                   
                   	
                   	$html = null;
                   }
                   
                   if (empty($title) && $pubmed && !empty($doi)) {

                        ##########	open esearch, fetch PMID	##########

                        $pmid = '';

                        $request_url = "http://www.ncbi.nlm.nih.gov/entrez/eutils/esearch.fcgi?db=Pubmed&term=".$doi."[AID]&usehistory=y&retstart=&retmax=1&sort=&tool=I,Librarian&email=i.librarian.software@gmail.com";

                        $xml = proxy_simplexml_load_file ($request_url, $_GET['proxy_name'], $_GET['proxy_port'], $_GET['proxy_username'], $_GET['proxy_password']);

                        if (empty($xml)) die('Error! I, Librarian could not connect with an external web service. This usually indicates that you access the Web through a proxy server.
                        Enter your proxy details in Tools->Settings. Alternatively, the external service may be temporarily down. Try again later.');

                        $count = $xml->Count;
                        if ($count == 1) $pmid = $xml->IdList->Id;

                        if (!empty($pmid)) {

                            ##########	open efetch, read xml	##########

                            $request_url = "http://www.ncbi.nlm.nih.gov/entrez/eutils/efetch.fcgi?db=Pubmed&rettype=abstract&retmode=XML&id=".$pmid."&tool=I,Librarian&email=i.librarian.software@gmail.com";

                            $xml = proxy_simplexml_load_file ($request_url, $_GET['proxy_name'], $_GET['proxy_port'], $_GET['proxy_username'], $_GET['proxy_password']);

                            if (empty($xml)) die('Error! I, Librarian could not connect with an external web service. This usually indicates that you access the Web through a proxy server.
                            Enter your proxy details in Tools->Settings. Alternatively, the external service may be temporarily down. Try again later.');

                            $uid			= 'PMID:'.$pmid;

                            $url			= "http://www.pubmed.org/$pmid";

                            $reference_type	= strtolower($xml->PubmedArticle->MedlineCitation->Article->PublicationTypeList->PublicationType[0]);

                            
                            $title       	= (string) $xml->PubmedArticle->MedlineCitation->Article->ArticleTitle;

                            $abstract_array = array();

                            $xml_abstract = $xml->PubmedArticle->MedlineCitation->Article->Abstract->AbstractText;

                            if(!empty($xml_abstract)) {
                                foreach ($xml_abstract as $mini_ab) {
                                    foreach($mini_ab->attributes() as $a => $b) {
                                        if ($a == 'Label') $mini_ab = $b.": ".$mini_ab;
                                    }
                                    $abstract_array[] = "$mini_ab";
                                }
                                $abstract = implode(' ', $abstract_array);
                            }

                            $secondary_title	= (string) $xml->PubmedArticle->MedlineCitation->Article->Journal->Title;

                            $day			= (string) $xml->PubmedArticle->MedlineCitation->Article->Journal->JournalIssue->PubDate->Day;
                            $month			= (string) $xml->PubmedArticle->MedlineCitation->Article->Journal->JournalIssue->PubDate->Month;
                            $year			= (string) $xml->PubmedArticle->MedlineCitation->Article->Journal->JournalIssue->PubDate->Year;

                            if (empty($year)) {
                                $year = (string) $xml->PubmedArticle->MedlineCitation->Article->Journal->JournalIssue->PubDate->MedlineDate;
                                preg_match ('/\d{4}/', $year, $year_match);
                                $year = $year_match[0];
                            }

                            if (!empty($year)) {
                                if(empty($day)) $day = '01';
                                if(empty($month)) $month = '01';
                                $year = date('Y-m-d', strtotime($day.'-'.$month.'-'.$year));
                            }

                            $volume    	  	= (string) $xml->PubmedArticle->MedlineCitation->Article->Journal->JournalIssue->Volume;

                            $issue                 = (string) $xml->PubmedArticle->MedlineCitation->Article->Journal->JournalIssue->Issue;

                            $pages     	 	= (string) $xml->PubmedArticle->MedlineCitation->Article->Pagination->MedlinePgn;

                            $journal 		= (string) $xml->PubmedArticle->MedlineCitation->MedlineJournalInfo->MedlineTA;

                            $affiliation         = (string) $xml->PubmedArticle->MedlineCitation->Article->Affiliation;

                            $authors		= $xml->PubmedArticle->MedlineCitation->Article->AuthorList->Author;

                            $name_array = array();
                            if (!empty($authors)) {
                                foreach ($authors as $author) {
                                    $name_array[] = 'L:"' . $author->LastName . '",F:"' . $author->ForeName . '"';
                                }
                            }
                            if (isset($name_array))
                                $authors = join(";", $name_array);

                            $mesh			= $xml->PubmedArticle->MedlineCitation->MeshHeadingList->MeshHeading;

                            if (!empty($mesh)) {
                                foreach ($mesh as $meshheading) {
                                    $mesh_array[] = $meshheading->DescriptorName;
                                }
                            }
                            if (isset($mesh_array)) $keywords = join (" / ", $mesh_array);
                        }
                    }
                    
                    if (empty($title) && $nasa && !empty($doi)) {
                    	
                        ############ NASA ADS ##############

                        $request_url = "http://adsabs.harvard.edu/cgi-bin/abs_connect?doi=".$doi."&data_type=XML&return_req=no_params&start_nr=1&nr_to_return=1";

                        $xml = proxy_simplexml_load_file ($request_url, $proxy_name, $proxy_port, $proxy_username, $proxy_password);

                        if (empty($xml)) die('Error! I, Librarian could not connect with an external web service. This usually indicates that you access the Web through a proxy server.
                        Enter your proxy details in Tools->Settings. Alternatively, the external service may be temporarily down. Try again later.');

                        foreach ($xml->attributes() as $a => $b) {

                            if ($a == 'selected') {
                                $count = $b;
                                break;
                            }
                        }

                        if ($count == 1) {

                            $record = $xml->record;

                            $bibcode = (string) $record->bibcode;
                            $title = (string) $record->title;

                            $journal = (string) $record->journal;
                            if (strstr($journal, ",")) $secondary_title = substr($journal, 0, strpos($journal, ','));

                            $eprintid = (string) $record->eprintid;
                            if (!empty($eprintid)) $eprintid = substr($eprintid, strpos($eprintid, ":")+1);
                            if (strstr($journal, "arXiv")) $eprintid = substr($journal, strpos($journal, ":")+1);

                            $volume = (string) $record->volume;
                            $pages = (string) $record->page;

                            $affiliation = $record->affiliation;

                            $year = (string) $record->pubdate;
                            $year = date('Y-m-d', strtotime($year));

                            $abstract = (string) $record->abstract;
                            $nasa_url = (string) $record->url;

                            foreach($record->link as $links) {

                                foreach ($links->attributes() as $a => $b) {

                                    if ($a == 'type' && $b == 'EJOURNAL') {
                                        $ejournal_url = (string) $links->url;
                                    } elseif ($a == 'type' && $b == 'PREPRINT') {
                                        $preprint_url = (string) $links->url;
                                    } elseif ($a == 'type' && $b == 'GIF') {
                                        $gif_url = (string) $links->url;
                                    }
                                }
                            }

                            $authors = $record->author;

                            $name_array = array();

                            if (!empty($authors)) {

                                foreach ($authors as $author) {
                                    $author_array = explode (",", $author);
                                    $name_array[] = 'L:"'.trim($author_array[0]).'",F:"'.trim($author_array[1]).'"';
                                }
                            }

                            if (isset($name_array))
                                $authors = join(";", $name_array);
                            
                            $keywords = $record->keywords;

                            if (!empty($keywords)) {

                                foreach ($keywords as $keyword) {

                                    $keywords_array[] = $keyword->keyword;
                                }
                            }

                            if (isset($keywords_array)) $keywords = join (" / ", $keywords_array);

                            $uid_array = array();
                            if (!empty($bibcode)) $uid_array[] = "NASAADS:$bibcode";
                            if (!empty($eprintid)) $uid_array[] = "ARXIV:$eprintid";
                            $uid = join("|", $uid_array);

                            $url_array = array();
                            $url_array[] = $nasa_url;
                            if (!empty($eprintid)) $url_array[] = "http://arxiv.org/abs/$eprintid";
                            $url = join("|", $url_array);
                        }
                    }
                    
                    if (empty($title) && $crossref && !empty($doi)) {

                        ############ CrossRef ##############

                        $request_url = "http://www.crossref.org/openurl/?id=doi:".$doi."&noredirect=true&pid=i.librarian.software@gmail.com";

                        $xml = proxy_simplexml_load_file ($request_url, $proxy_name, $proxy_port, $proxy_username, $proxy_password);

                        if (empty($xml)) die('Error! I, Librarian could not connect with an external web service. This usually indicates that you access the Web through a proxy server.
                        Enter your proxy details in Tools->Settings. Alternatively, the external service may be temporarily down. Try again later.');

                        $record = $xml->query_result->body->query;

                        $resolved = false;

                        foreach ($record->attributes() as $a => $b) {

                            if ($a == 'status' && $b == 'resolved') {
                                $resolved = true;
                                break;
                            }
                        }

                        if ($resolved) {

                            $secondary_title = (string) $record->journal_title;
                            $year = (string) $record->year;
                            $year = $year.'-01-01';
                            $volume = (string) $record->volume;
                            $pages = (string) $record->first_page;
                            $last_page = (string) $record->last_page;
                            if (!empty($last_page)) $pages = $pages."-".$last_page;
                            $title = (string) $record->article_title;
                            $authors = array ();
                            foreach ($record->contributors->contributor as $contributor) {
                                $authors1 = (string) $contributor->surname;
                                $authors2 = (string) $contributor->given_name;
                                $authors[] = 'L:"' . $authors1 . '",F:"' . $authors2 . '"';
                            }
                            $authors = join(";", $authors);
                        }
                    }

                    
                    //TRY AGAIN WITH DOI ONE CHARACTER SHORTER
                    if (empty($title)) {
                    	
                    $doi = substr($doi, 0, -1);
                    
                    
                    if(empty($title) && $ieee && !empty($doi)){
                    	
                    	$request_url = "http://ieeexplore.ieee.org/gateway/ipsSearch.jsp?doi=".urlencode($doi);
                    
                    	$xml = proxy_simplexml_load_file($request_url, $proxy_name, $proxy_port, $proxy_username, $proxy_password);
                    	if (empty($xml)) $count = 0;
                    
                    	if(!isset($count))
                    		$count = $xml->totalfound;
                    
                    	$record = $xml->document;
                    
                    	if(!empty($record->title))
                    		$title = (string)$record->title;
                    
                    	if(!empty($record->doi))
                    		$doi = $record->doi;
                    
                    	if(!empty($record->py)){
                    		$pub_date = $record->py."-01-01";
                    		$year = $pub_date;
                    	}
                    
                    	if(!empty($record->affiliations))
                    		$affiliation = $record->affiliations;
                    	//$_POST['secondary_title'] = $record->pubtitle;
                    	if(!empty($record->pubtitle))
                    		$journal = $record->pubtitle;
                    	if(!empty($record->issue))
                    		$issue = $record->issue;
                    	if(!empty($record->volume))
                    		$volume = $record->volume;
                    	if(!empty($record->spage) && !empty($record->epage))
                    		$pages = $record->spage."-".$record->epage;
                    	if(!empty($record->publisher))
                    		$publisher = $record->publisher;
                    	if(!empty($record->pubtype))
                    		$reference_type = $record->pubtype;
                    	if(!empty($record->abstract))
                    		$abstract = trim($record->abstract);
                    
                    	if(!empty($record->aturhors))
                    		$authors = (string)$record->authors;
                    
                    	$name_array = array();
                    	if (!empty($authors)) {
                    
                    		$author_array = explode(';', $authors);
                    		foreach($author_array as $author){
                    			$exp = explode(',',$author);
                    			$last = trim(array_pop($exp));
                    			$first = trim(array_pop($exp));
                    			$name_array[] = 'L:"'.$last.'",F:"'.$first.'"';
                    		}
                    	}
                    
                    	if (isset($name_array))
                    		$authors = join(";", $name_array);
                    
                    	$m=0;
                    	$suggested_keywords = array();
                    	if(isset($xml->document->controlledterms) || !empty($xml->document->controlledterms)){
                    		foreach($xml->document->controlledterms->term as $term){
                    			$suggested_keywords[$m] = (string)$term;
                    			$m++;
                    		}
                    	}
                    
                    
                    	if(isset($xml->document->thesaurusterms) || !empty($xml->document->thesaurusterms)){
                    		foreach($xml->document->thesaurusterms->term as $term){
                    			if(!in_array((string)$term, $suggested_keywords)){
                    				$suggested_keywords[$m]=(string)$term;
                    			}
                    			$m++;
                    		}
                    	}
                    
                    	if(isset($xml->document->uncontrolledterms) || !empty($xml->document->uncontrolledterms)){
                    		foreach($xml->document->uncontrolledterms->term as $term){
                    			if(!in_array((string)$term, $suggested_keywords)){
                    				$suggested_keywords[$m]=(string)$term;
                    			}
                    			$m++;
                    		}
                    	}
                    
                    	if(isset($suggested_keywords)) $keywords = join (" / ", $suggested_keywords);
                    
                    	if(!empty($title)){
                    		if(!empty($record->arnumber))
                    			$uid = "IEEE:$record->arnumber";
                    		if(!empty($record->mdurl))
                    			$url = (string)$record->mdurl;
                    		//$_POST['form_new_file_link'] = (string)$record->pdf;
                    	}
                    }
                    
                    
                    
                    if(empty($title) && $acm && !empty($doi)){
                    	if(!empty($doi))
                    		// Trim the doi to omit the part before "/"
                    		if (stripos($doi, '/') > 0) $acm_id = trim(substr($doi,stripos($doi, '/')+1));
                    
                    	$client = new apiClient();
                    	$client->setApplicationName('Google CustomSearch PHP Starter Application');
                    	// Docs: http://code.google.com/apis/customsearch/v1/using_rest.html
                    	// Visit https://code.google.com/apis/console?api=customsearch to generate
                    	// your developer key (simple api key).
                    	$client->setDeveloperKey('AIzaSyCnyVXlI3vtzXji5Skyq2tjB151GKuxHTM');
                    	$search = new apiCustomsearchService($client);
                    
                    	try{
                    		//Get the result from the Google Search
                    		$result = $search->cse->listCse($doi, array(
                    				'cx' => '003464685527199486674:rgmxgh34f6u', // The custom search engine ID to scope this search query.
                    		));
                    	} catch (apiServiceException $e) {
                    		// Handle exception. You can also catch Exception here.
                    		// You can also get the error code from $e->getCode();
                    		//die($e->getCode());
                    	}
                    
                    	//Get the metadata from the search.
                    	// nb! levenshtein function allows 0 symbols to differ
                    	// Daniil: MODIFICATION BEGIN
                    	$j = 0;
                    	$n=$result['queries']['request'][0]['count'];
                    
                    	while ($j<$n){
                    		//echo $result['items'][$i];
                    		//echo $i;
                    		$metadata_array = $result['items'][$j]['pagemap']['metatags'][0];
                    
                    		//var_dump($metadata_array);
                    		if(isset($metadata_array['citation_doi'])){
                    			$doi_search = $metadata_array['citation_doi'];
                    			$uid_search = substr($metadata_array['citation_doi'], strpos($metadata_array['citation_doi'],"/")+1);
                    		}
                    
                    
                    		if (levenshtein($doi_search, $doi)==0 || (!empty($uid) && levenshtein($uid_search,$uid)==0)) {
                    			// Daniil: MODIFICATION END
                    			$uid = "ACM:$acm_id";
                    
                    			$count =1;
                    			if(isset($metadata_array['citation_title'])){
                    				$title= $metadata_array['citation_title'];
                    			}
                    			if(isset($metadata_array['citation_authors'])){
                    				$authors = $metadata_array['citation_authors'];
                    
                    				$name_array = array();
                    				if (!empty($authors)) {
                    
                    					$author_array = explode(';', $authors);
                    					foreach($author_array as $author){
                    						$exp = explode(',',$author);
                    						$first = trim(array_pop($exp));
                    						$last = trim(array_pop($exp));
                    						$name_array[] = 'L:"'.$last.'",F:"'.$first.'"';
                    					}
                    				}
                    
                    				if (isset($name_array))
                    					$authors = join(";", $name_array);
                    			}
                    
                    			if(isset($metadata_array['citation_journal_title']))
                    				$yournal = $metadata_array['citation_journal_title'];
                    			if(isset($metadata_array['citation_date']))
                    				$year = date('Y-m-d',strtotime($metadata_array['citation_date']));
                    			if(isset($metadata_array['citation_volume']))
                    				$volume = $metadata_array['citation_volume'];
                    			if(isset($metadata_array['citation_issue']))
                    				$issue = $metadata_array['citation_issue'];
                    			if(isset($metadata_array['citation_firstpage']))
                    				$tmp_pages = $metadata_array['citation_firstpage'];
                    			if(isset($metadata_array['citation_lastpage']))
                    				$tmp_pages = $tmp_pages."-".$metadata_array['citation_lastpage'];
                    			$pages= $tmp_pages;
                    
                    			if(isset($metadata_array['citation_publisher']))
                    				$publisher = $metadata_array['citation_publisher'];
                    			if(isset($metadata_array['citation_doi']))
                    				$doi = $metadata_array['citation_doi'];
                    
                    			if(isset($metadata_array['citation_abstract_html_url']))
                    				$url = $metadata_array['citation_abstract_html_url'];
                    
                    			$suggested_keywords = array();
                    			if(isset($metadata_array['citation_keywords'])){
                    				$keyword_array = explode($keyword_separator, $metadata_array['citation_keywords']);
                    				//function trim_value(&$value) {
                    				//	$value = trim($value);
                    				//}
                    				array_walk($keyword_array, 'trim_value');
                    				$m=0;
                    				foreach ($keyword_array as $k){
                    					$suggested_keywords[$m] = $k;
                    					$m++;
                    				}
                    
                    			}
                    			if (isset($suggested_keywords)) $keywords = join (" / ", $suggested_keywords);
                    
                    		}
                    
                    		$j++;
                    	}
                    }
                    
                    if(empty($title) && $springerlink && !empty($doi)){
                    
                    	$request_url = "http://www.springerlink.com/content/?k=doi:(".$doi.")";
                    
                    	// Create DOM from URL or file
                    	$html = file_get_html($request_url);
                    
                    
                    	// Find the page of the article for given doi
                    	foreach($html->find('ul[id=PrimaryManifest]') as $ul)
                    	{
                    		foreach($ul->find('li') as $li)
                    		{
                    			foreach($li->find('p.title a') as $p){
                    				$page=$p->href;
                    			}
                    		}
                    	}
                    
                    	$html = null;
                    
                    	$request_url = "http://springerlink.com".$page;
                    	// Create DOM from URL or file
                    	$html = file_get_html($request_url);
                    
                    	$doi1 = $html->find('span.doi span.value');
                    	if(!empty($doi1))
                    		$doi = $doi1[0]->plaintext;
                    
                    	$title1 = $html->find('div.text h1 a');
                    	if(!empty($title1)){
                    		$title = $title1[0]->plaintext;
                    		$count = 0;
                    	}
                    
                    	$journal1 = $html->find('div.primary a');
                    	if(!empty($journal1))
                    		$journal = $journal1[0]->plaintext;
                    
                    	$pages1 = $html->find('span.pagination');
                    	if(!empty($pages1))
                    		$pages = $pages1[0]->plaintext;
                    
                    	//Get the authors
                    	$authors_array = array();
                    	foreach($html->find('div.text p.authors') as $d){
                    		//$doi=$d->find('span');
                    		foreach($d->children as $c)
                    			$authors_array[]=$c->plaintext."<br>";
                    	}
                    	$authors1 = implode('; ', $authors_array);
                    	$authors1 = str_replace("<br>", "", $authors);
                    
                    	$name_array = array();
                    	if (!empty($authors1)) {
                    
                    		$author_array = explode(';', $authors1);
                    		foreach($author_array as $author1){
                    			$exp = explode(' ',$author);
                    			$first = trim(array_pop($exp));
                    			$last = trim(array_pop($exp));
                    			$name_array[] = 'L:"'.$last.'",F:"'.$first.'"';
                    		}
                    	}
                    
                    	if (isset($name_array))
                    		$authors = join(";", $name_array);
                    
                    
                    	$html = null;
                    }
                    
                    if ($pubmed && empty($title) && !empty($doi)) {

                        ##########	open esearch, fetch PMID	##########

                        $pmid = '';

                        $request_url = "http://www.ncbi.nlm.nih.gov/entrez/eutils/esearch.fcgi?db=Pubmed&term=".$doi."[AID]&usehistory=y&retstart=&retmax=1&sort=&tool=I,Librarian&email=i.librarian.software@gmail.com";

                        $xml = proxy_simplexml_load_file ($request_url, $_GET['proxy_name'], $_GET['proxy_port'], $_GET['proxy_username'], $_GET['proxy_password']);

                        $count = $xml->Count;
                        if ($count == 1) $pmid = $xml->IdList->Id;

                        if (!empty($pmid)) {

                            ##########	open efetch, read xml	##########

                            $request_url = "http://www.ncbi.nlm.nih.gov/entrez/eutils/efetch.fcgi?db=Pubmed&rettype=abstract&retmode=XML&id=".$pmid."&tool=I,Librarian&email=i.librarian.software@gmail.com";

                            $xml = proxy_simplexml_load_file ($request_url, $_GET['proxy_name'], $_GET['proxy_port'], $_GET['proxy_username'], $_GET['proxy_password']);

                            $uid			= 'PMID:'.$pmid;

                            $url			= "http://www.pubmed.org/$pmid";

                            $reference_type	= strtolower($xml->PubmedArticle->MedlineCitation->Article->PublicationTypeList->PublicationType[0]);

                            $title       	= (string) $xml->PubmedArticle->MedlineCitation->Article->ArticleTitle;

                            $abstract_array = array();

                            $xml_abstract = $xml->PubmedArticle->MedlineCitation->Article->Abstract->AbstractText;

                            if(!empty($xml_abstract)) {
                                foreach ($xml_abstract as $mini_ab) {
                                    foreach($mini_ab->attributes() as $a => $b) {
                                        if ($a == 'Label') $mini_ab = $b.": ".$mini_ab;
                                    }
                                    $abstract_array[] = "$mini_ab";
                                }
                                $abstract = implode(' ', $abstract_array);
                            }

                            $secondary_title	= (string) $xml->PubmedArticle->MedlineCitation->Article->Journal->Title;

                            $day			= (string) $xml->PubmedArticle->MedlineCitation->Article->Journal->JournalIssue->PubDate->Day;
                            $month			= (string) $xml->PubmedArticle->MedlineCitation->Article->Journal->JournalIssue->PubDate->Month;
                            $year			= (string) $xml->PubmedArticle->MedlineCitation->Article->Journal->JournalIssue->PubDate->Year;

                            if (empty($year)) {
                                $year = (string) $xml->PubmedArticle->MedlineCitation->Article->Journal->JournalIssue->PubDate->MedlineDate;
                                preg_match ('/\d{4}/', $year, $year_match);
                                $year = $year_match[0];
                            }

                            if (!empty($year)) {
                                if(empty($day)) $day = '01';
                                if(empty($month)) $month = '01';
                                $year = date('Y-m-d', strtotime($day.'-'.$month.'-'.$year));
                            }

                            $volume    	  	= (string) $xml->PubmedArticle->MedlineCitation->Article->Journal->JournalIssue->Volume;

                            $issue                 = (string) $xml->PubmedArticle->MedlineCitation->Article->Journal->JournalIssue->Issue;

                            $pages     	 	= (string) $xml->PubmedArticle->MedlineCitation->Article->Pagination->MedlinePgn;

                            $journal 		= (string) $xml->PubmedArticle->MedlineCitation->MedlineJournalInfo->MedlineTA;

                            $affiliation         = (string) $xml->PubmedArticle->MedlineCitation->Article->Affiliation;

                            $authors		= $xml->PubmedArticle->MedlineCitation->Article->AuthorList->Author;

                            $name_array = array();
                            if (!empty($authors)) {
                                foreach ($authors as $author) {
                                    $name_array[] = 'L:"' . $author->LastName . '",F:"' . $author->ForeName . '"';
                                }
                            }
                            if (isset($name_array))
                                $authors = join(";", $name_array);

                            $mesh			= $xml->PubmedArticle->MedlineCitation->MeshHeadingList->MeshHeading;

                            if (!empty($mesh)) {
                                foreach ($mesh as $meshheading) {
                                    $mesh_array[] = $meshheading->DescriptorName;
                                }
                            }

                            if (isset($mesh_array)) $keywords = join (" / ", $mesh_array);
                        }
                    }

                    if (empty($title) && $nasa && !empty($doi)) {

                        ############ NASA ADS ##############

                        $request_url = "http://adsabs.harvard.edu/cgi-bin/abs_connect?doi=".$doi."&data_type=XML&return_req=no_params&start_nr=1&nr_to_return=1";

                        $xml = proxy_simplexml_load_file ($request_url, $proxy_name, $proxy_port, $proxy_username, $proxy_password);

                        foreach ($xml->attributes() as $a => $b) {

                            if ($a == 'selected') {
                                $count = $b;
                                break;
                            }
                        }

                        if ($count == 1) {

                            $record = $xml->record;

                            $bibcode = (string) $record->bibcode;
                            $title = (string) $record->title;

                            $journal = (string) $record->journal;
                            if (strstr($journal, ",")) $secondary_title = substr($journal, 0, strpos($journal, ','));

                            $eprintid = (string) $record->eprintid;
                            if (!empty($eprintid)) $eprintid = substr($eprintid, strpos($eprintid, ":")+1);
                            if (strstr($journal, "arXiv")) $eprintid = substr($journal, strpos($journal, ":")+1);

                            $volume = (string) $record->volume;
                            $pages = (string) $record->page;

                            $affiliation = $record->affiliation;

                            $year = (string) $record->pubdate;
                            $year = date('Y-m-d', strtotime($year));

                            $abstract = (string) $record->abstract;
                            $nasa_url = (string) $record->url;

                            foreach($record->link as $links) {

                                foreach ($links->attributes() as $a => $b) {

                                    if ($a == 'type' && $b == 'EJOURNAL') {
                                        $ejournal_url = (string) $links->url;
                                    } elseif ($a == 'type' && $b == 'PREPRINT') {
                                        $preprint_url = (string) $links->url;
                                    } elseif ($a == 'type' && $b == 'GIF') {
                                        $gif_url = (string) $links->url;
                                    }
                                }
                            }

                            $authors = $record->author;

                            $name_array = array();

                            if (!empty($authors)) {

                                foreach ($authors as $author) {
                                    $author_array = explode (",", $author);
                                    $name_array[] = 'L:"'.trim($author_array[0]).'",F:"'.trim($author_array[1]).'"';
                                }
                            }

                            if (isset($name_array))
                                $authors = join(";", $name_array);

                            $keywords = $record->keywords;

                            if (!empty($keywords)) {

                                foreach ($keywords as $keyword) {

                                    $keywords_array[] = $keyword->keyword;
                                }
                            }

                            if (isset($keywords_array)) $keywords = join (" / ", $keywords_array);

                            $uid_array = array();
                            if (!empty($bibcode)) $uid_array[] = "NASAADS:$bibcode";
                            if (!empty($eprintid)) $uid_array[] = "ARXIV:$eprintid";
                            $uid = join("|", $uid_array);

                            $url_array = array();
                            $url_array[] = $nasa_url;
                            if (!empty($eprintid)) $url_array[] = "http://arxiv.org/abs/$eprintid";
                            $url = join("|", $url_array);
                        }
                    }

                    if (empty($title) && $crossref && !empty($doi)) {

                        ############ CrossRef ##############

                        $request_url = "http://www.crossref.org/openurl/?id=doi:".$doi."&noredirect=true&pid=i.librarian.software@gmail.com";

                        $xml = proxy_simplexml_load_file ($request_url, $proxy_name, $proxy_port, $proxy_username, $proxy_password);

                        $record = $xml->query_result->body->query;

                        $resolved = false;

                        foreach ($record->attributes() as $a => $b) {

                            if ($a == 'status' && $b == 'resolved') {
                                $resolved = true;
                                break;
                            }
                        }

                        if ($resolved) {

                            $secondary_title = (string) $record->journal_title;
                            $year = (string) $record->year;
                            $year = $year.'-01-01';
                            $volume = (string) $record->volume;
                            $pages = (string) $record->first_page;
                            $last_page = (string) $record->last_page;
                            if (!empty($last_page)) $pages = $pages."-".$last_page;
                            $title = (string) $record->article_title;
                            $authors = array ();
                            foreach ($record->contributors->contributor as $contributor) {
                                $authors1 = (string) $contributor->surname;
                                $authors2 = (string) $contributor->given_name;
                                $authors[] = 'L:"' . $authors1 . '",F:"' . $authors2 . '"';
                            }
                            $authors = join(";", $authors);
                        }
                    }
                    }
                	}
                   
                    if (empty($title)) {

                            if (isset($_POST['failed']) && $_POST['failed']=='1') {
								if(!empty($title_pdf)){
									$title = $title_pdf;
										
								}
                              /*  $string = preg_replace ($patterns, ' ', $string);
                                $string = preg_replace ('/(^|\s)\S{1,2}(\s|$)/', ' ', $string);
                                $string = preg_replace ('/\s{2,}/', " ", $string);

                                $fulltext_array = array();
                                $fulltext_unique = array();

                                $fulltext_array = explode(" ", $string);
                                $fulltext_unique = array_unique($fulltext_array);
                                $string = implode(" ", $fulltext_unique);

                                database_connect($database_path, 'library');
                                record_unknown($dbHandle, $string, $database_path, $file, $userID);

                                $put = " ($i) ".basename($orig_filename).": Recorded into category !unknown. No database record found.<br>";
							*/
                            } else {

                                $put = " ($i) ".basename($orig_filename).": No database record found.<br>";
                            }
                    }

                    if (!empty($title)) {
                    	
                    	if(empty($url) || !isset($url))
                    		$url = '';
                    	if(empty($authors) || !isset($authors))
                    		$authors = '';
                    	if(empty($affiliation) || !isset($affiliation))
                    		$affiliation = '';
                    	
                    	if(empty($abstract) || !isset($abstract))
                    		$abstract = '';
                    	if(empty($secondary_title) || !isset($secondary_title))
                    		$secondary_title = '';
                    	if(empty($year) || !isset($year))
                    		$year = '';
                    	if(empty($volume) || !isset($volume))
                    		$volume = '';
                    	if(empty($issue) || !isset($issue))
                    		$issue = '';
                    	if(empty($pages) || !isset($pages))
                    		$pages = '';
                    	
                    	$last_page = '';
                    	if(empty($journal) || !isset($journal))
                    		$journal = '';
                    	if(empty($keywords) || !isset($keywords))
                    		$keywords = '';
                    	
                    	
                    	$addition_date = date('Y-m-d');
                    	
                    	$rating = 2;
                    	if(empty($uid) || !isset($uid))
                    		$uid = '';
                    	if(empty($editor) || !isset($editor))
                    		$editor = '';
                    	if(empty($reference_type) || !isset($reference_type))
                    		$reference_type = '';
						if(empty($publisher) || !isset($publisher))
                    		$publisher = '';
                    	if(empty($place_published) || !isset($place_published))
							$place_published = '';
                    	if(empty($doi) || !isset($doi))
                    		$doi = '';
                    	if(empty($authors_ascii) || !isset($authors_ascii))
                    		$authors_ascii = '';
                    	if(empty($title_ascii) && !isset($title_ascii))
                    		$title_ascii = '';
                    	if(empty($abstract_ascii) || !isset($abstract_ascii))
                    		$abstract_ascii = '';
                    	
                    	 
                    	
                        database_connect($database_path, 'library');

                        if (!empty($authors)) $authors_ascii = utf8_deaccent($authors);

                        $title_ascii = utf8_deaccent($title);
                        
                        if (!empty($abstract)) $abstract_ascii = utf8_deaccent($abstract);

                        ##########	record publication data, table library	##########

                        $query = "INSERT INTO library (file, authors, affiliation, title, journal, year, addition_date, abstract, rating, uid, volume, issue, pages, secondary_title, editor,
                            url, reference_type, publisher, place_published, keywords, doi, authors_ascii, title_ascii, abstract_ascii, added_by)
                            VALUES ((SELECT IFNULL((SELECT SUBSTR('0000' || CAST(MAX(file)+1 AS TEXT) || '.pdf',-9,9) FROM library),'00001.pdf')), :authors, :affiliation, :title, :journal, :year, :addition_date, :abstract, :rating, :uid, :volume, :issue, :pages, :secondary_title, :editor,
                            :url, :reference_type, :publisher, :place_published, :keywords, :doi, :authors_ascii, :title_ascii, :abstract_ascii, :added_by)";

                        $stmt = $dbHandle->prepare($query);

                        $stmt->bindParam(':authors', $authors, PDO::PARAM_STR);
                        $stmt->bindParam(':affiliation', $affiliation, PDO::PARAM_STR);
                        $stmt->bindParam(':title', $title, PDO::PARAM_STR);
                        $stmt->bindParam(':journal', $journal, PDO::PARAM_STR);
                        $stmt->bindParam(':year', $year, PDO::PARAM_STR);
                        $stmt->bindParam(':addition_date', $addition_date, PDO::PARAM_STR);
                        $stmt->bindParam(':abstract', $abstract, PDO::PARAM_STR);
                        $stmt->bindParam(':rating', $rating, PDO::PARAM_INT);
                        $stmt->bindParam(':uid', $uid, PDO::PARAM_STR);
                        $stmt->bindParam(':volume', $volume, PDO::PARAM_STR);
                        $stmt->bindParam(':issue', $issue, PDO::PARAM_STR);
                        $stmt->bindParam(':pages', $pages, PDO::PARAM_STR);
                        $stmt->bindParam(':secondary_title', $secondary_title, PDO::PARAM_STR);
                        $stmt->bindParam(':editor', $editor, PDO::PARAM_STR);
                        $stmt->bindParam(':url', $url, PDO::PARAM_STR);
                        $stmt->bindParam(':reference_type', $reference_type, PDO::PARAM_STR);
                        $stmt->bindParam(':publisher', $publisher, PDO::PARAM_STR);
                        $stmt->bindParam(':place_published', $place_published, PDO::PARAM_STR);
                        $stmt->bindParam(':keywords', $keywords, PDO::PARAM_STR);
                        $stmt->bindParam(':doi', $doi, PDO::PARAM_STR);
                        $stmt->bindParam(':authors_ascii', $authors_ascii, PDO::PARAM_STR);
                        $stmt->bindParam(':title_ascii', $title_ascii, PDO::PARAM_STR);
                        $stmt->bindParam(':abstract_ascii', $abstract_ascii, PDO::PARAM_STR);
                        $stmt->bindParam(':added_by', $userID, PDO::PARAM_INT);
                        
                        $dbHandle->exec("BEGIN IMMEDIATE TRANSACTION");

                        $stmt->execute();
                        
                        	$error_code = $stmt->errorCode();
                        	
                      
                        $error_array = 	$dbHandle->errorInfo();
                      //  die($error_array[2]);
                        $stmt = null;

                        $last_insert = $dbHandle->query("SELECT last_insert_rowid(),max(file) FROM library");
                        $last_row = $last_insert->fetch(PDO::FETCH_ASSOC);
                        $last_insert = null;
                        $id = $last_row['last_insert_rowid()'];
                        $new_file = $last_row['max(file)'];
                        
                        
                        
                        if (isset($_GET['shelf']) && !empty($userID)) {
                            $user_query = $dbHandle->quote($userID);
                            $file_query = $dbHandle->quote($id);
                            $dbHandle->exec("INSERT OR IGNORE INTO shelves (userID,fileID) VALUES ($user_query,$file_query)");
                        }
                        

                        if (isset($_GET['project']) && !empty($_GET['projectID'])) {
                            $dbHandle->exec("INSERT OR IGNORE INTO projectsfiles (projectID,fileID) VALUES (".intval($_GET['projectID']).",".intval($id).")");
                        }

                        ####### record new category into categories, if not exists #########

                        if (!empty($_GET['category2'])) {

                            $category_ids = array();

                            $_GET['category2'] = preg_replace('/\s{2,}/', '', $_GET['category2']);
                            $_GET['category2'] = preg_replace('/^\s$/', '', $_GET['category2']);
                            $_GET['category2'] = array_filter($_GET['category2']);

                            $query = "INSERT INTO categories (category) VALUES (:category)";
                            $stmt = $dbHandle->prepare($query);
                            $stmt->bindParam(':category', $new_category, PDO::PARAM_STR);

                            while (list($key,$new_category) = each($_GET['category2'])) {
                                $new_category_quoted = $dbHandle->quote($new_category);
                                $result = $dbHandle->query("SELECT categoryID FROM categories WHERE category=$new_category_quoted");
                                $exists = $result->fetchColumn();
                                $category_ids[] = $exists;
                                $result = null;
                                if(empty($exists)) {
                                    $stmt->execute();
                                    $last_id = $dbHandle->query("SELECT last_insert_rowid() FROM categories");
                                    $category_ids[] = $last_id->fetchColumn();
                                    $last_id = null;
                                }
                            }
                            $stmt = null;
                        }

                        ####### record new relations into filescategories #########

                        $categories = array();

                        if (!empty($_GET['category']) || !empty($category_ids)) {
                            $categories = array_merge((array)$_GET['category'], (array)$category_ids);
                            $categories = array_filter(array_unique($categories));
                        }

                        $query = "INSERT OR IGNORE INTO filescategories (fileID,categoryID) VALUES (:fileid,:categoryid)";

                        $stmt = $dbHandle->prepare($query);
                        $stmt->bindParam(':fileid', $id);
                        $stmt->bindParam(':categoryid', $category_id);

                        while (list($key,$category_id) = each($categories)) {
                            if (!empty($id)) $stmt->execute();
                        }
                        $stmt = null;

                        $dbHandle->exec("COMMIT");
                        $dbHandle = null;
                        
                        $string = preg_replace ($patterns, ' ', $string);
                        $string = preg_replace ('/(^|\s)\S{1,2}(\s|$)/', ' ', $string);
                        $string = preg_replace ('/\s{2,}/', " ", $string);

                        $fulltext_array = array();
                        $fulltext_unique = array();

                        $fulltext_array = explode(" ", $string);
                        $fulltext_unique = array_unique($fulltext_array);
                        $string = implode(" ", $fulltext_unique);

                        database_connect($database_path, 'fulltext');

                        $file_query = $dbHandle->quote($id);
                        $fulltext_query = $dbHandle->quote($string);

                        $dbHandle->beginTransaction();
                        $dbHandle->exec("DELETE FROM full_text WHERE fileID=$file_query");
                        $insert = $dbHandle->exec("INSERT INTO full_text (fileID,full_text) VALUES ($file_query,$fulltext_query)");
                        $dbHandle->commit();

                        

                        copy ($file, dirname(__FILE__).DIRECTORY_SEPARATOR."library".DIRECTORY_SEPARATOR.$new_file);
                        
                        $unpack_dir = $temp_dir.DIRECTORY_SEPARATOR.'i-librarian'.DIRECTORY_SEPARATOR.$new_file;
                        @mkdir($unpack_dir);
                        exec(select_pdftk().'"'.$library_path.DIRECTORY_SEPARATOR.$new_file.'" unpack_files output "'.$unpack_dir.'"');
                        $unpacked_files = scandir($unpack_dir);
                        foreach ($unpacked_files as $unpacked_file) {
                            @rename ($unpack_dir.DIRECTORY_SEPARATOR.$unpacked_file, $library_path.DIRECTORY_SEPARATOR.supplement.DIRECTORY_SEPARATOR.sprintf ("%05d", intval($new_file)).$unpacked_file);
                        }
                        @rmdir($unpack_dir);
                        if(!empty($id)){
	                        $no_success ++;
	                        //$put = basename($orig_filename).": Recorded.<br>";
	                        if($not_found){
	                        	$html_log_2 .= "<li>" . basename($orig_filename) . ": Recorded with metadata from the PDF. DOI not found.</li>";
	                        	$log .= "[" . $timestamp . "] " . basename($orig_filename) . ": Recorded with metadata from the PDF. DOI not foun.!\n";
	                        }else{
		                        $html_log_2 .= "<li>" . basename($orig_filename) . ": Recorded.</li>";
		                        $log .= "[" . $timestamp . "] " . basename($orig_filename) . ": Recorded.\n";
	                        }
                        }else{
                        	
                        	$no_failed ++;
                        	$html_log_2 .= "<li>" . basename($orig_filename) . ": Not Recorded. An error occured.</li>";
                        	if(!empty($error_code))
                        		$log .= "[" . $timestamp . "] " . basename($orig_filename) . ": Not recorded. An error occured.".$error_code." - ".$error_array[2]."\n";
                        	else
                        		$log .= "[" . $timestamp . "] " . basename($orig_filename) . ": Not recorded. An error occured.\n";
                        }
                        $dbHandle = null;
                    }
                
            }
        } else {
        	$html_log_2 .= "<li>" . basename($orig_filename) . ": Not readable.</li>";
        	$log .= "[" . $timestamp . "] " . basename($orig_filename) . ": Not readable.\n";
           // $put = basename($orig_filename).": Not readable.<br>";
        }
    } ####if

    ##########  ANALYZE  ##########
    database_connect($database_path, 'library');
    $dbHandle->exec("ANALYZE");
    $dbHandle = null;

    ###### clean the temp directory ########
    	for ($j=$i; $j>=1; $j--) {

        	if (file_exists($temp_dir.DIRECTORY_SEPARATOR."librarian_temp".$j.".txt")) unlink ($temp_dir.DIRECTORY_SEPARATOR."librarian_temp".$j.".txt");
    	}
    	 }
     }
     if (!empty($html_log_2)) {
     	//Write a log file
     	$timestamp2 = date("Y-m-d H:i:s");
     	$endtime = time();
     	$log .= "\n\nBatch Import finished at ".$timestamp2;
     	$log .= "\n$i PDF processed. $no_success files successfuly imported, $no_failed files failed.";
     	$log .= "\nThe import took ".round(abs($endtime - $starttime) / 60,2). " minutes";
     	$t = mktime();
     	
     	$logfile = "library/batch/log" . $t . ".txt";
     	$fh = fopen($logfile, 'w') or die("can't open file to write the final log");
     	fwrite($fh, $log);
     	fclose($fh);
     	
     	if($i>20)
     		$html_log_2 = "Batch Import finished. <br>See the detail log <a href='library/batch/log".$t.".txt' target='_blank'>here</a>";
     	else{
     		$html_log_2  = "<ol>".$html_log_2."</ol>";
     		$html_log_2 .= "<br>See the detail log <a href='library/batch/log" . $t . ".txt' target='_blank'>here</a>";
     	}
     
    	die($html_log_2);
    
     }
}

if (isset($_SESSION['auth']) && ($_SESSION['permissions'] == 'A' || $_SESSION['permissions'] == 'U')) {

    $proxy_name = '';
    $proxy_port = '';
    $proxy_username = '';
    $proxy_password = '';

    if (isset($_SESSION['connection']) && ($_SESSION['connection'] == "autodetect" || $_SESSION['connection'] == "url")) {
        if(!empty($_GET['proxystr'])) {
            $proxy_arr = explode (';', $_GET['proxystr']);
            foreach ($proxy_arr as $proxy_str) {
                if (stripos(trim($proxy_str), 'PROXY') === 0) {
                    $proxy_str = trim(substr ($proxy_str, 6));
                    $proxy_name = parse_url($proxy_str, PHP_URL_HOST);
                    $proxy_port = parse_url($proxy_str, PHP_URL_PORT);
                    $proxy_username = parse_url($proxy_str, PHP_URL_USER);
                    $proxy_password = parse_url($proxy_str, PHP_URL_PASS);
                    break;
                }
            }
        }
    } else {
        if(isset($_SESSION['proxy_name'])) $proxy_name = $_SESSION['proxy_name'];
        if(isset($_SESSION['proxy_port'])) $proxy_port = $_SESSION['proxy_port'];
        if(isset($_SESSION['proxy_username'])) $proxy_username = $_SESSION['proxy_username'];
        if(isset($_SESSION['proxy_password'])) $proxy_password = $_SESSION['proxy_password'];
    }

            database_connect($usersdatabase_path, 'users');
            $batchimport_database_pubmed = get_setting($dbHandle,'batchimport_database_pubmed');
            $batchimport_database_nasaads = get_setting($dbHandle,'batchimport_database_nasaads');
            $batchimport_database_crossref = get_setting($dbHandle,'batchimport_database_crossref');
            $batchimport_failed = get_setting($dbHandle,'batchimport_failed');
            $dbHandle = null;
    ?>
<div style="margin:4px;font-weight:bold">PDF Batch Import</div>
<form id="batchimportform2" action="batch_noflash.php" method="POST" enctype="multipart/form-data">
    <input type="hidden" name="commence" value="1">
    <input type="hidden" name="user" value="<?php print htmlspecialchars($_SESSION['user']); ?>">
    <input type="hidden" name="userID" value="<?php print  htmlspecialchars($_SESSION['user_id']); ?>">
    <input type="hidden" name="proxy_name" value="<?php print  htmlspecialchars($proxy_name); ?>">
    <input type="hidden" name="proxy_port" value="<?php print  htmlspecialchars($proxy_port); ?>">
    <input type="hidden" name="proxy_username" value="<?php print  htmlspecialchars($proxy_username); ?>">
    <input type="hidden" name="proxy_password" value="<?php print  htmlspecialchars($proxy_password); ?>">
    <table cellspacing="0" style="width: 100%;border-top: solid 1px #D5D6D9">
         <tr>
            <td valign="top" class="threedright" colspan="2">
                <div id="uploaderOverlay" style="position:absolute; z-index:2"></div>
                <div style="float:left">
                <input id="select-button" type="file" multiple="multiple" 
                value="Select Files" name="files[]" onChange="makeFileList();" />
            	</div><div style="padding:5px">
                You selected <span id="file-count">0 files</span>.
                (Note that PDFs must contain a <a href="http://en.wikipedia.org/wiki/Digital_object_identifier" target="_blank">DOI</a> in order to track the corresponding metadata.)
           	</div>
            </td>
        </tr>
        <tr>
            <td valign="top" class="threedleft">
                <button id="import-button2" >Import</button>
            </td>
            <td valign="top" class="threedright">
                <table cellspacing=0>
                    <tr>
                        <td class="select_span" style="line-height:22px;width:10em">
                            <input type="checkbox" checked class="uploadcheckbox" style="display:none" name="shelf">
                            <span class="ui-icon ui-icon-check" style="float:left;margin-top: 2px">
                            </span>Add to Shelf
                        </td>
                        <td class="select_span" style="line-height:22px;width: 10em;text-align:right">
                            <input type="checkbox" class="uploadcheckbox" style="display:none" name="project">
                            <div style="float:right">Add&nbsp;to&nbsp;Project&nbsp;</div>
                            <span class="ui-icon ui-icon-close" style="float:right;margin-top: 2px">
                            </span>
                        </td>
                        <td style="line-height:22px;width: 18em">
                            <select name="projectID" style="width:200px">
                                    <?php

                                    database_connect($database_path, 'library');

                                    $id_query = $dbHandle->quote($_SESSION['user_id']);

                                    $result = $dbHandle->query("SELECT DISTINCT projects.projectID,project FROM projects
                            LEFT OUTER JOIN projectsusers ON projects.projectID=projectsusers.projectID
                            WHERE projects.userID=$id_query OR projectsusers.userID=$id_query ORDER BY project COLLATE NOCASE ASC");

                                    while($project = $result->fetch(PDO::FETCH_ASSOC)) {
                                        print '<option value="'.$project['projectID'].'">'.htmlspecialchars($project['project']).'</option>'.PHP_EOL;
                                    }

                                    $result = null;
                                    $dbHandle = null;

                                    ?>
                            </select>
                        </td>
                    </tr>
                </table>
            </td>
        </tr>
    </table>
    <table cellspacing="0" style="width: 100%" id="table1">
        <tr><td class="threedleft">Select database:</td>
            <td class="threedright">
                <table cellspacing="0">
                    <tr>
                        <td class="select_span">
                        	<input type="checkbox" checked class="uploadcheckbox" style="display:none" name="database[]" value="ieee">
                        	<span class="ui-icon ui-icon-check" style="float:left;margin-top: 2px">
                        	</span>IEEE (Computer science
                       </td>
                     </tr>
                     <tr>
                        <td class="select_span">
                        	<input type="checkbox" checked class="uploadcheckbox" style="display:none" name="database[]" value="acm">
                        	<span class="ui-icon ui-icon-check" style="float:left;margin-top: 2px">
                        	</span>ACM (Computer Science)
                       </td>
                     </tr>
                     <tr>
                     <tr>
                        <td class="select_span">
                        	<input type="checkbox" class="uploadcheckbox" style="display:none" name="database[]" value="springerlink">
                        	<span class="ui-icon ui-icon-close" style="float:left;margin-top: 2px">
                        	</span>Springerlink (Not recomended, very slow)
                       </td>
                     </tr>
                     <tr>
                        <td class="select_span">
                        	<input type="checkbox" class="uploadcheckbox" style="display:none" name="database[]" value="pubmed">
                        	<span class="ui-icon ui-icon-close" style="float:left;margin-top: 2px">
                        	</span>PubMed (biomedicine)
                       </td>
                     </tr>
                     <tr>
                        <td class="select_span">
                        	<input type="checkbox" class="uploadcheckbox" style="display:none" name="database[]" value = "nasa">
                        	<span class="ui-icon ui-icon-close" style="float:left;margin-top: 2px">
                        	</span>NASA ADS (physics, astronomy)
                       </td>
                     </tr>
                        <td class="select_span">
                        	<input type="checkbox" class="uploadcheckbox" style="display:none" name="database[]" value = "crossref">
                        	<span class="ui-icon ui-icon-close" style="float:left;margin-top: 2px">
                        	</span>CrossRef (other sciences)
                       </td>
                     </tr>
                </table>
            </td></tr>
        <tr><td class="threedleft">If metadata not found:</td>
            <td class="threedright" style="line-height: 16px">
                <table cellspacing="0">
                    <tr>
                        <td class="select_span" style="line-height: 16px">
                            <input type="radio" name="failed" value="1" style="display: none" <?php print (isset($batchimport_failed) && $batchimport_failed=='1') ? 'checked' : '' ?>>
                            <span class="ui-icon ui-icon-radio-<?php print (isset($batchimport_failed) && $batchimport_failed=='1') ? 'off' : 'on' ?>" style="float:left"></span>
                            Import the PDF with the data extracted from the PDF. All PDF files will be recorded and indexed!
                        </td></tr>
                    <tr>
                        <td class="select_span" style="line-height: 16px">
                            <input type="radio" name="failed" value="2" style="display: none" <?php print (empty($batchimport_failed) || (isset($batchimport_failed) && $batchimport_failed=='2')) ? 'checked' : '' ?>>
                            <span class="ui-icon ui-icon-radio-<?php print (empty($batchimport_failed) || (isset($batchimport_failed) && $batchimport_failed=='2')) ? 'off' : 'on' ?>" style="float:left"></span>
                            Do not record the PDF, but copy it to<br>&nbsp;&nbsp;&nbsp;&nbsp; <?php print dirname(__FILE__).DIRECTORY_SEPARATOR.'library'.DIRECTORY_SEPARATOR.'batch';   ?>
                        </td>
                    </tr>
                    <tr>
                        <td class="select_span" style="line-height: 16px">
                            <input type="radio" name="failed" value="3" style="display: none" <?php print (isset($batchimport_failed) && $batchimport_failed=='3') ? 'checked' : '' ?>>
                            <span class="ui-icon ui-icon-radio-<?php print (isset($batchimport_failed) && $batchimport_failed=='3') ? 'off' : 'on' ?>" style="float:left"></span>
                            Do not record the PDF, do not copy it.
                        </td>
                    </tr></table>
            </td>
        </tr></table>
    <table cellspacing="0" style="width:100%" id="table2">
        <tr>
            <td class="threedleft">
                Choose&nbsp;category:<br>
            </td>
            <td class="threedright">
                <div id="categorydiv" style="width: 99%;overflow:scroll; height: 400px;background-color: white;color: black;border: 1px solid #C5C6C9">
                    <table cellspacing=0 style="float:left;width: 49%">
                            <?php
                            $category_string = null;
                            database_connect($database_path,'library');
                            $result = $dbHandle->query("SELECT count(*) FROM categories");
                            $totalcount = $result->fetchColumn();
                            $result = null;

                            $j=1;
                            $isdiv = null;
                            $result = $dbHandle->query("SELECT categoryID,category FROM categories ORDER BY category COLLATE NOCASE ASC");
                            while ($category = $result->fetch(PDO::FETCH_ASSOC)) {
                                if ($j > (1+$totalcount/2) && !$isdiv) {
                                    print '</table><table cellspacing=0 style="width: 49%;float: right;padding:2px">';
                                    $isdiv = true;
                                }
                                print PHP_EOL.'<tr><td class="select_span">';
                                print "<input type=\"checkbox\" name=\"category[]\" value=\"".htmlspecialchars($category['categoryID'])."\"";
                                print " style=\"display:none\"><span  class=\"ui-icon ui-icon-close\" style=\"float:left\"></span>".htmlspecialchars($category['category'])."</td></tr>";
                                $j=$j+1;
                            }
                            $result = null;
                            $dbHandle = null;
                            ?>
                    </table>
                </div>
                <span class="ui-icon ui-icon-triangle-1-s" style="cursor: pointer;float:left" id="enlargelist"></span>
                <span class="ui-icon ui-icon-triangle-1-n" style="cursor: pointer;" id="shrinklist"></span>
            </td>
        </tr>
        <tr>
            <td class="threedleft">
                Add to new categories:
            </td>
            <td class="threedright">
                <input type="text" size="30" name="category2[]" value=""><br>
                <input type="text" size="30" name="category2[]" value=""><br>
                <input type="text" size="30" name="category2[]" value="">
            </td>
        </tr>
    </table>
</form>
<br>
    <?php
}
?>